package demo.practice.binary_tree;

import demo.practice.TreeNode;

public class Offer54 {


    public int kthLargest(TreeNode root, int k) {
        int[] c = new int[]{0};
        return midfor(root, c, k);
    }

    public int midfor(TreeNode root, int[] c, int k) {

        if (root == null) {
            return -1;
        }
        if (root.right != null) {
            int v = midfor(root.right, c, k);
            if (v != -1)
                return v;
        }
        c[0] += 1;
        if (c[0] == k)
            return root.val;

        if (root.left != null) {
            return midfor(root.left, c, k);
        }
        return -1;
    }

}
